import data from './emoji-data';
let emojiData = {};
Object.values(data).forEach((item: any) => {
  emojiData = { ...emojiData, ...item };
});

const allimgReg = /<img.*?(?:>|\/>)/gi; //全局
const imgReg = /<img.*?(?:>|\/>)/i; //第一个就停止
const srcReg = /key=[\'\"]?([^\'\"]*)[\'\"]?/gi;
/**
 *
 *
 * @export
 * @param {string} value
 * @returns {string}
 */

export const emoji = (value: string) => {
  if (!value) return '';

  Object.keys(emojiData).forEach(item => {
    value = value.replace(new RegExp(`${item}`, 'g'), createIcon(item));
  });
  return value;
};

export const UnEmoji = (value: any) => {
  if (!value) return '';
  console.log('起始:', value);
  var arr = value.match(allimgReg);
  if (!arr) return value;
  debugger;
  for (var i = 0; i < arr.length; i++) {
    var temp = arr[i].match(srcReg);
    if (temp && temp.length > 0) {
      var regex = new RegExp(`(?<=").*?(?=")`, 'g');
      value = value.replace(imgReg, temp[0].match(regex)[0]);
    }
  }

  // value = value.replace(imgReg, value.match(srcReg)[1]);

  return value;
};

export const createIcon = (item: any, path: string = '') => {
  const value = emojiData[item];
  const imgName = item.split('-')[0].substring(1);
  let src = '';

  if (!path) src = 'https://qiniu.lvzhenhome.cn/' + imgName + value;
  return `<img key="${item}" class="emoji-img" src='${src}' />`;
};

export default {
  emoji,
  UnEmoji,
  createIcon
};
